package com.coolbi.security.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.coolbi.common.util.CommonUtil;
import com.coolbi.common.util.Md5Util;
import com.coolbi.security.biz.UserBiz;
import com.coolbi.security.dao.UserDao;
import com.coolbi.security.dao.UserToGroupDao;
import com.coolbi.security.entity.User;
import com.coolbi.security.exception.DBOperateException;

public class UserBizImpl implements UserBiz {
	
	@Autowired
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Autowired
	private UserToGroupDao userToGroupDao;

	@Override
	public List<User> find(Map<String, Object> parameter) {
		return userDao.find(parameter);
	}

	@Override
	public List<User> find(Map<String, Object> parameter, int offset,
			int limit) {
		return userDao.find(parameter,offset,limit);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return userDao.count();
	}

	@Override
	public int countByKeyword(String keyword) {
		return userDao.countByKeyword(keyword);
	}

	@Override
	public List<User> search(String keyword, int offset, int limit) {
		return userDao.search(keyword, offset, limit);
	}

	@Override
	public User save(User obj) {
		return userDao.save(obj);
	}

	@Override
	public int update(User obj) {
		return userDao.update(obj);
	}

	@Override
	public int delete(long id) {
		return userDao.delete(id);
	}

	@Override
	public User get(long id) {
		return userDao.get(id);
	}

	@Override
	public List<User> list() {
		return userDao.list();
	}

	@Override
	public List<User> list(int offset, int limit) {
		return userDao.list(offset, limit);
	}

	@Override
	public int count() {
		return userDao.count();
	}

	@Override
	public boolean existByTel(String tel) {
		return userDao.countByTel(tel) > 0;
	}

	@Override
	public boolean existByEmail(String email) {
		return userDao.countByEmail(email) > 0;
	}

	@Override
	public boolean existByUserName(String username) {
		return userDao.countByUserName(username) > 0;
	}

	@Override
	public boolean updatePassword(String password, long id) {
		return userDao.updatePassword(password, id) > 0;
	}

	@Override
	public int lockedUser(long id, boolean isLock) {
		return userDao.lockedUser(id, isLock);
	}

	@Override
	public User findUserByUsername(String username) {
		return userDao.findUserByUsername(username);
	}

	@Override
	public void saveByBatch(User user, String groups) {
		user.setPassword(Md5Util.getMD5Str(user.getPassword()));
		User resultUser = userDao.save(user);
		if (null == resultUser) {
			throw new DBOperateException("管理员添加失败");
		}
		List<Long> groupIds = CommonUtil.parseToList(groups, ",");
		if (userToGroupDao.saveByBatch(resultUser.getId(), groupIds) < 1) {
			throw new DBOperateException("用户组添加失败");
		}
	}

	public void setUserToGroupDao(UserToGroupDao userToGroupDao) {
		this.userToGroupDao = userToGroupDao;
	}

	@Override
	public void updateByBatch(User user, String groups) {
		if (userDao.update(user) < 1) {
			throw new DBOperateException("管理员修改失败");
		}
		userToGroupDao.delete(user.getId());
		List<Long> groupIds = CommonUtil.parseToList(groups, ",");
		if (userToGroupDao.saveByBatch(user.getId(), groupIds) < 1) {
			throw new DBOperateException("用户组添加失败");
		}
	}

	@Override
	public String findUserName() {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		if (null == securityContext) {
			return null;
		}
		if (null == securityContext.getAuthentication()) {
			return null;
		}
		UserDetails userDetails = (UserDetails) securityContext.getAuthentication().getPrincipal();
		if (null == userDetails) {
			return null;
		}
		return userDetails.getUsername();
	}

	@Override
	public List<User> findAllUserNames() {
		return userDao.findAllUserNames();
	}
}
